package com.sita.bike;

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;

public class MyDaoGenerator {
    public static void main(String[] args) throws Exception {
        int version = Integer.valueOf(args[0]);
        String outputDir = args[1];
        Schema schema = new Schema(version, "com.sita.bike.persistence");
        addPersonCredential(schema);
        addFriends(schema);
        addRouteTrackData(schema);

        addRtResource(schema);
        addRtBlacklist(schema);
        addRtNote(schema);

        addCcDriver(schema);
        addCcDriverTickets(schema);
        addCcPassengerTickets(schema);

        addGroupChatDetailEvent(schema);
        addMembers(schema);
        addTrendMember(schema);
        new DaoGenerator().generateAll(schema, outputDir);
    }

    private static void addPersonCredential(Schema schema) {
        Entity person = schema.addEntity("Person");
        person.addIdProperty().columnName("_id").primaryKey();
        person.addStringProperty("mobile");
        person.addStringProperty("name");
        person.addStringProperty("email");
        person.addStringProperty("address");
        person.addStringProperty("avatarUrl");
        person.addByteArrayProperty("photo");

        Entity credential = schema.addEntity("Credential");
        credential.addIdProperty().columnName("_id").primaryKey();
//        credential.addStringProperty("salt").notNull(); // changed 11-23 mark, no need
//        credential.addStringProperty("uuid").notNull();
        credential.addStringProperty("token");
        credential.addStringProperty("xmppUser").columnName("xmpp_user").notNull();
        credential.addStringProperty("xmppPass").columnName("xmpp_pass").notNull();
        credential.addDateProperty("lastLoginTime").columnName("last_login_time").notNull();

        Property personId = credential.addLongProperty("personId").unique().notNull().getProperty();
        credential.addToOne(person, personId);

        person.implementsSerializable();
        credential.implementsSerializable();
    }

    private static void addFriends(Schema schema) {
        Entity friend = schema.addEntity("Friend");
        friend.addIdProperty().columnName("_id").primaryKey();
        friend.addStringProperty("nickname");
        friend.addStringProperty("mobile");
        friend.addStringProperty("gender");
        friend.addStringProperty("avatarUrl");
        friend.addByteArrayProperty("photo");
        friend.implementsSerializable();
    }

    private static void addRtResource(Schema schema){
        Entity entity = schema.addEntity("RtResourceEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("title");
        entity.addStringProperty("trafficType");
        entity.addStringProperty("accountId");
        entity.addLongProperty("createDate").indexDesc("idx_rt_res_crdt", false);
        entity.addStringProperty("pics");
        entity.addStringProperty("thumbnails");
        entity.addStringProperty("orgpicname");
        entity.addStringProperty("audios");
        entity.addStringProperty("shortVideo");
        entity.addStringProperty("fullVideo");
        entity.addStringProperty("location");
        entity.addLongProperty("voteCount");
        entity.implementsSerializable();
    }

    private static void addRtNote(Schema schema){
        Entity entity = schema.addEntity("RtNoteEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("accountId");
        entity.addStringProperty("resourceId");
        entity.addStringProperty("message");
        entity.addLongProperty("createTime").indexDesc("idx_rt_note_crtm", false);
        entity.implementsSerializable();
    }

    private static void addRtBlacklist(Schema schema){
        Entity entity = schema.addEntity("RtBlacklistEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("blockUser");
        entity.implementsSerializable();
    }

    private static void addCcDriver(Schema schema){
        Entity entity = schema.addEntity("CcDriverEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("vehicleNumber");
        entity.addStringProperty("vehicleBrand");
        entity.implementsSerializable();
    }

    private static void addCcDriverTickets(Schema schema){
        Entity entity = schema.addEntity("CcDriverTicketEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("driverId");
        entity.addStringProperty("passengerId");
        entity.addStringProperty("fromAddress");
        entity.addStringProperty("toAddress");
        entity.addStringProperty("fromLocation");
        entity.addStringProperty("toLocation");
        entity.addLongProperty("departureTime");
        entity.addFloatProperty("money");
        entity.addIntProperty("status");
        entity.implementsSerializable();
    }

    private static void addCcPassengerTickets(Schema schema){
        Entity entity = schema.addEntity("CcPassengerTicketEntity");
        entity.addIdProperty().primaryKeyDesc();
        entity.addStringProperty("driverId");
        entity.addStringProperty("passengerId");
        entity.addStringProperty("fromAddress");
        entity.addStringProperty("toAddress");
        entity.addStringProperty("fromLocation");
        entity.addStringProperty("toLocation");
        entity.addLongProperty("departureTime");
        entity.addFloatProperty("money");
        entity.addIntProperty("status");
        entity.implementsSerializable();
    }

    private static void addRouteTrackData(Schema schema) {
        Entity tracks = schema.addEntity("Tracks");
        tracks.addIdProperty().primaryKey();
        tracks.implementsSerializable();

        Entity route = schema.addEntity("Route");
        route.addIdProperty().columnName("_id").primaryKey();
        route.addStringProperty("description");
        route.addLongProperty("startTime");
        route.addLongProperty("endTime");
        route.addLongProperty("distance"); // meters
        route.addIntProperty("averageRpm"); // 1000x
        route.addIntProperty("maxRpm"); // 1000x
        route.addFloatProperty("maxSpeed"); // m/s
        route.addStringProperty("startLoc"); // lat,long
        route.addStringProperty("endLoc"); // lat,long
        route.addStringProperty("startAddress");
        route.addStringProperty("endAddress");
        route.addStringProperty("syncStatus"); // TBD sync status flag, NOT_UPLOAD; UPLOADED; NOT_DOWNLOAD;DOWNLOADED
        route.addStringProperty("filePath");
        route.addStringProperty("serverRouteId"); // server returned

        Entity routeData = schema.addEntity("RouteData");
        routeData.addIdProperty().columnName("_id").primaryKey();
        routeData.addDoubleProperty("latitude");
        routeData.addDoubleProperty("longitude");
        routeData.addLongProperty("time");
        routeData.addFloatProperty("speed");
        routeData.addIntProperty("rpm");
        routeData.addIntProperty("shift");
        routeData.addIntProperty("routeId");

//        Property tracksgpsId = gpsLocations.addLongProperty("tracksgpsId").unique().notNull().getProperty();
//        gpsLocations.addToMany(tracks, tracksgpsId);

        // create new table for speed, round, oil
        Entity bluetoothdata = schema.addEntity("BluetoothData");
        bluetoothdata.addIdProperty().columnName("_id").primaryKey();
        bluetoothdata.addIntProperty("speed");
        bluetoothdata.addIntProperty("round");
        bluetoothdata.addIntProperty("oil");

//        Property tracksblueId = bluetoothdata.addLongProperty("tracksblueId").unique().notNull().getProperty();
//        bluetoothdata.addToMany(tracks, tracksblueId);

        route.implementsSerializable();
        routeData.implementsSerializable();
        bluetoothdata.implementsSerializable();
    }

//    private static void addContact(Schema schema){
//        Entity contact = schema.addEntity("Contact");
//        contact.addIdProperty().columnName("_id").primaryKey();
//        contact.addStringProperty("phone");
//        contact.addStringProperty("nickName");
//        contact.addStringProperty("city");
//        contact.addByteArrayProperty("photo");
//        contact.implementsSerializable();
//    }
//
//    private static void addPendingContact(Schema schema){
//        Entity contact = schema.addEntity("PendingContact");
//        contact.addIdProperty().columnName("_id").primaryKey();
//        contact.addStringProperty("phone");
//        contact.addStringProperty("nickName");
//        contact.addStringProperty("city");
//        contact.addByteArrayProperty("photo");
//        contact.addIntProperty("status");
//        contact.implementsSerializable();
//    }

    private static  void addGroupChatDetailEvent(Schema schema){
        Entity groupChatDetailEvent = schema.addEntity("GroupChatDetailEvent");
        groupChatDetailEvent.addIdProperty();
        groupChatDetailEvent.addStringProperty("groupid");
        groupChatDetailEvent.addStringProperty("targetid");
        groupChatDetailEvent.implementsSerializable();
    }

    private static void addMembers(Schema schema) {
        Entity friend = schema.addEntity("GroupMember");
        friend.addIdProperty().columnName("_id").primaryKey();
        friend.addStringProperty("nickname");
        friend.addStringProperty("gender");
        friend.addStringProperty("avatarUrl");
        friend.implementsSerializable();
    }
    private static void addTrendMember(Schema schema) {
        Entity friend = schema.addEntity("TrendMember");
        friend.addIdProperty().columnName("accountId").primaryKey();
        friend.addStringProperty("nickname");
        friend.addStringProperty("gender");
        friend.addStringProperty("avatarUrl");
        friend.implementsSerializable();
    }
}
